{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {
    "collapsed": true
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "(5,)\n",
      "1\n",
      "2\n",
      "[-1  2  3  4  5]\n"
     ]
    }
   ],
   "source": [
    "import numpy as np\n",
    "my_array = np.array([1,2,3,4,5])\n",
    "# 它会打印我们创建的数组的形状：(5, )。意思就是 my_array 是一个包含5个元素的数组。\n",
    "print(my_array.shape)\n",
    "\n",
    "# 我们也可以打印各个元素。就像普通的Python数组一样，NumPy数组的起始索引编号为0。\n",
    "print(my_array[0])\n",
    "print(my_array[1])\n",
    "\n",
    "# 修改numpy数组中某个元素的值,跟list是一样的\n",
    "my_array[0] = -1\n",
    "print(my_array)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### np.zeros((几行，几列))  np.ones((几行，几列))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "metadata": {
    "collapsed": true
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[0. 0. 0. 0. 0.]\n",
      "[[0. 0. 0.]\n",
      " [0. 0. 0.]]\n",
      "[[1. 1. 1.]\n",
      " [1. 1. 1.]]\n"
     ]
    }
   ],
   "source": [
    "import numpy as np\n",
    "\n",
    "my_new_array = np.zeros((5))\n",
    "print(my_new_array)\n",
    "my_new_array2 = np.zeros((2,3))\n",
    "print(my_new_array2)\n",
    "my_new_array3 = np.ones((2,3))\n",
    "print(my_new_array3)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### np.random.random因为我们使用的是随机函数，它为每个元素分配0到1之间的随机值。"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "metadata": {
    "scrolled": true
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[0.66111964 0.26018924 0.86032019 0.27857554 0.99361875]\n"
     ]
    }
   ],
   "source": [
    "# 创建随机数组\n",
    "my_random_array = np.random.random((5))\n",
    "print(my_random_array)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### 多维数组的元素的输出可以用下标来表示"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 13,
   "metadata": {
    "scrolled": true
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[4 5]\n",
      "5\n",
      "(2, 2)\n"
     ]
    }
   ],
   "source": [
    "my_array = np.array([[4,5],\n",
    "                     [6,7]])\n",
    "print(my_array[0])  # 输出[4 5]\n",
    "print(my_array[0][1])  # 输出第0行第1个元素单个元素5\n",
    "print(my_array.shape)  # 输出矩阵的形状"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### 假设，我们想从中提取第二列（索引1）的所有元素。在这里，我们肉眼可以看出，第二列由两个元素组成：5 和 1。为此，我们可以执行以下操作数组名[:,1]表示任意行中第二列（索引值为1）"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 15,
   "metadata": {
    "scrolled": true
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[5 7]\n"
     ]
    }
   ],
   "source": [
    "my_array_column2 = my_array[:,1]\n",
    "print(my_array_column2)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### numpy数组的操作\n",
    "### +、-、*、/ 为数组中每一个对应的元素相加减\n",
    "### 矩阵的乘法运算为np.dot(矩阵a,矩阵b)或者 矩阵a.dot(矩阵b)，两者的计算结果是一样的"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 19,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "sum =  [[ 6.  8.]\n",
      " [10. 12.]]\n",
      "sub =  [[-4. -4.]\n",
      " [-4. -4.]]\n",
      "mult =  [[ 5. 12.]\n",
      " [21. 32.]]\n",
      "div =  [[0.2        0.33333333]\n",
      " [0.42857143 0.5       ]]\n",
      "--------数组的矩阵运算-----\n",
      "[[19. 22.]\n",
      " [43. 50.]]\n",
      "[[19. 22.]\n",
      " [43. 50.]]\n"
     ]
    }
   ],
   "source": [
    "import numpy as np\n",
    "a = np.array([[1.0,2.0],\n",
    "              [3.0,4.0]])\n",
    "b = np.array([[5.0,6.0],\n",
    "              [7.0,8.0]])\n",
    "\n",
    "#  +、-、*、/ 为数组中每一个对应的元素相加减\n",
    "sum = a + b\n",
    "sub = a - b\n",
    "mult = a * b\n",
    "div = a / b\n",
    "print('sum = ', sum)\n",
    "print('sub = ', sub)\n",
    "print('mult = ', mult)\n",
    "print('div = ', div)\n",
    "\n",
    "# 矩阵的乘法运算为np.dot(矩阵a,矩阵b)或者 矩阵a.dot(矩阵b)，两者的计算结果是一样的\n",
    "matrix_mult = np.dot(a, b)\n",
    "matrix_mult2 = a.dot(b)\n",
    "print(\"--------数组的矩阵运算-----\")\n",
    "print(matrix_mult)\n",
    "print(matrix_mult2)"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.6.4"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
